package com.example.lab05.util;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

public class UploadUtil {

    public static String savePicture(MultipartFile multipartFile, String directory) {
        File path = new File(directory); // 上传文件存放的位置
        String extension = StringUtils.getFilenameExtension(multipartFile.getOriginalFilename()); // 扩展名  png, jpg, ...
        String filename = UUID.randomUUID() + "." + extension; // 重定义唯一文件名
        try {
            multipartFile.transferTo(new File(path, filename)); // 保存文件
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return filename;
    }
    public static String savePicture(MultipartFile[] multipartFiles,String directory){
        String[] filenames = new String[multipartFiles.length];
        for(int i =0;i< multipartFiles.length;i++){
            filenames[i] = "\""+savePicture(multipartFiles[i],directory)+"\"";
        }
        return Arrays.toString(filenames);//数组的形式按照字符串的形式输出。
    }
}
